package com.example.demo3.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalHandlerException {


    /**
     *  认证 账户 异常处理
     * @param e
     * @return
     */
    @ExceptionHandler(UnknownAccountException.class)
    public Object  UnknownAccountExceptionHandler(UnknownAccountException e ){
        log.info(e.getMessage());
        return " 用户名不存在 "+e.getMessage() ;
    }

    /**
     *  认证 密码 异常处理
     * @param e
     * @return
     */
    @ExceptionHandler(AuthenticationException.class)
    public Object  AuthenticationExceptionHandler(AuthenticationException e ){
        log.info(e.getMessage());
        return  " 用户名或密码错误： " + e.getMessage() ;
    }


    /**
     *  权限异常处理
     * @param e
     * @return
     */
    @ExceptionHandler(AuthorizationException.class)
    public Object  AuthorizationExceptionHandler(AuthorizationException e ){
        log.info(e.getMessage());
        return " 用户没有权限：  "+e.getMessage() ;
    }

    /**
     *  基础异常处理
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public Object  ExceptionHandler(Exception e ){
        log.info(e.getMessage());
        return e.getMessage() ;
    }



}
